﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NoteList.Models;
using NoteList.Delegates;
using NoteList.EvtArgs;

namespace NoteList.Forms
{
    public partial class EditForm : Form
    {
        public event NoteChanged OnNoteChanged;

        private ZNote _note = null;
        private bool _hasNoteChange = false;
        private bool _isNewNote = false;

        public EditForm()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.iconotelist;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            HideForm();
        }

        public void ShowForm(Form pForm, ZNote note)
        {
            _note = note;
            _isNewNote = (_note == null);
            RefreshValue();
            this.StartPosition = (pForm.WindowState == FormWindowState.Minimized) ? FormStartPosition.CenterScreen : FormStartPosition.CenterParent;
            this.ShowDialog(pForm);
        }

        public void HideForm()
        {
            this.Close();
        }

        private void RefreshValue()
        {
            if (_note == null)
            {
                tbTitle.Text = "";
                tbContent.Text = "";
                dlAlarmSetting.SelectedIndex = 2;
            }
            else
            {
                tbTitle.Text = _note.Title;
                tbContent.Text = _note.Content;
                tbAlarmTime.Text = _note.AlarmTime.ToString("yyyy-MM-dd HH:mm");
                dlAlarmSetting.SelectedIndex = dlAlarmSetting.Items.Count - 1;
            }
            SetDefaultTimeText();
        }

        private void ChangeNote(ZNote note)
        {
            if (note == null)
                note = new ZNote();
            note.Title = tbTitle.Text;
            note.Content = tbContent.Text;
            note.EnableAlarm = (dlAlarmSetting.SelectedIndex > 0);
            note.AlarmTime = DateTime.Parse(tbAlarmTime.Text);
            note.LastEditTime = DateTime.Now;
        }

        private void UserInputChanged(object sender, EventArgs e)
        {
            _hasNoteChange = true;
        }

        private void btSaveItem_Click(object sender, EventArgs e)
        {
            if (_hasNoteChange)
            {
                NoteChangeEventArgs evt = new NoteChangeEventArgs();
                evt.IsNewNote = _isNewNote;
                if (_isNewNote)
                    _note = new ZNote();
                ChangeNote(_note);
                evt.Note = _note;
                OnNoteChanged.Invoke(this, evt);
            }
            HideForm();
        }

        private void EditForm_Shown(object sender, EventArgs e)
        {
            tbTitle.Focus();
            tbTitle.SelectAll();
            SetDefaultTimeText();
        }

        private void dlAlarmSetting_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetDefaultTimeText();
        }

        private void SetDefaultTimeText()
        {
            DateTime alarmTime = DateTime.Today;
            bool tb = DateTime.TryParse(tbAlarmTime.Text, out alarmTime);
            tbAlarmTime.Enabled = false;
            switch (dlAlarmSetting.SelectedIndex)
            {
                case 0: break;
                case 1: alarmTime = DateTime.Now.AddMinutes(5); break;
                case 2: alarmTime = DateTime.Now.AddHours(1); break;
                case 3: alarmTime = DateTime.Today.AddHours(17); break;
                case 4: alarmTime = DateTime.Today.AddDays(1).AddHours(9); break;
                case 5: tbAlarmTime.Enabled = true; break;
            }
            tbAlarmTime.Text = alarmTime.ToString("yyyy-MM-dd HH:mm");
            _hasNoteChange = true;
        }
    }
}
